<template>
	<view class="dizhi">
		<radio-group @change="radioChange">
			<view class="list" v-for="(item,index) in list" :key="item.id" >
				<view class="top" @click="seladdress(index)">
					<view>{{item.name}}</view>
					<view>{{item.phone}}</view>
				</view>
				<view class="address" @click="seladdress(index)">{{item.address}}({{item.addressDetail}})</view>
				<view class="bottom">
					<label class="">
							<radio :value="item.id" :checked="item.isDefault==1" color="#284179" /><text class="default">设为默认</text>
					</label>
					<view style="display: flex;">
						<view class="b_right" @click="edit(item.id)">
							<image class="bj_img" src="/static/images/bianji_03.png" mode=""></image>
							<view class="text">编辑</view>
						</view>
						<view class="b_right" @click="del(item.id)">
							<image class="bj_img" src="/static/images/del_05.png" mode=""></image>
							<view class="text">删除</view>
						</view>
					</view>
				</view>
			</view>
		</radio-group>
		<navigator url="/pages/tianjia_dizhi/tianjia_dizhi" class="tj_add">
			+ 添加地址
		</navigator>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				list:[],
				url:this.weburl+'&c=user&a=mydizhi',
				userinfo:[],
				delurl:this.weburl+'&c=user&a=deldizhi',
				defaluturl:this.weburl+'&c=user&a=defalutaddress',
				from:0
			}
		},
		onLoad(options) {
			this.userinfo=uni.getStorageSync('user');
			this.from = options.from;
		},
		onShow() {
			uni.setNavigationBarTitle({
			    title: '地址管理'
			});
			this.getinfo();
		},
		methods:{
			radioChange(e){
				
				var that=this;
				this.$http.get('user/setAddress',{'id':e.detail.value}).
				then(function (response) {
					that.getinfo();
				}).catch(function (error) {
					console.log(error);
				});
				
				
			},
			seladdress(index){
				if(this.from == 1){
					uni.setStorageSync('addressinfo', this.list[index]);
					uni.navigateBack();
				}
			},
			getinfo(){
				var that=this;
				console.log(that.userinfo);
				this.$http.get('user/getUserAddress',{}).
				then(function (response) {
					that.list=response.data;
				}).catch(function (error) {
					console.log(error);
				});
			},
			del(id){
				var that=this;
				uni.showModal({
				    title: '删除地址',
				    content: '确认删除吗？不可撤回',
				    success: function (res) {
				        if (res.confirm) {
				            that.$http.get('user/delAddress',{'id':id}).
				            then(function (response) {
				            	that.getinfo();
				            }).catch(function (error) {
				            	console.log(error);
				            });
				        }
				    }
				});
			},
			edit(id){
				uni.navigateTo({
					url:'/pages/editaddress/editaddress?id='+id
				})
			}
		}
	}
	
</script>

<style>
	page{
		background-color: #f7f7f7;
	}
</style>

<style scoped lang="scss">
	.default{
		color: #a5a5a5;
		font-size: 30rpx;
	}
	.uni-radio-input{
		width: 50rpx;
		height: 50rpx;
	}
	.dizhi{
		.list{
			margin-top: 20rpx;
			padding: 0 20rpx;
			background-color: #FFFFFF;
			&:first-child{
				margin: 0;
			}
			.top{
				padding: 40rpx 0 15rpx 0;
				display: flex;
				justify-content: space-between;
				color: #000000;
				font-size: 30rpx;
			}
			.address{
				color: #a5a5a5;
				font-size: 28rpx;
				padding-bottom: 30rpx;
				border-bottom: 1px solid #ebebeb;
			}
			.bottom{
				display: flex;
				height: 80rpx;
				align-items: center;
				justify-content: space-between;
				// justify-content: flex-end;
				.b_right{
					margin-right: 20rpx;
					display: flex;
					height: 80rpx;
					align-items: center;
					.bj_img{
						width: 28rpx;
						height: 28rpx;
					}
					.text{
						margin-left: 20rpx;
						color: #a5a5a5;
						font-size: 24rpx;
					}
				}
			}
		}
		.tj_add{
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: #284179;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 36rpx;
			color: #FFFFFF;
		}
	}
</style>
